package com.yiqi.news.mvp.ui.fragment

import android.os.Bundle
import android.os.Message
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import butterknife.OnClick

import com.jess.arms.base.BaseFragment
import com.jess.arms.di.component.AppComponent
import com.jess.arms.utils.ArmsUtils
import com.jess.arms.utils.RxClipboardTool
import com.socks.library.KLog
import com.yiqi.huize.loadImage

import com.yiqi.news.di.component.DaggerUserComponent
import com.yiqi.news.di.module.UserModule
import com.yiqi.news.mvp.contract.UserContract
import com.yiqi.news.mvp.presenter.UserPresenter

import com.yiqi.news.R
import com.yiqi.news.app.EventBusTags
import com.yiqi.news.app.base.ActivityLifeCycle.getTodayStartTime
import com.yiqi.news.app.service.UserController
import com.yiqi.news.app.utils.SPUtil
import com.yiqi.news.app.utils.SPUtil.SP_NAME
import com.yiqi.news.app.widget.qmui.QMUITipDialog
import com.yiqi.news.entity.User
import com.yiqi.news.mvp.ui.activity.*
import kotlinx.android.synthetic.main.fragment_user.*
import org.simple.eventbus.Subscriber


class UserFragment : BaseFragment<UserPresenter>(), UserContract.View {
    override fun showReadSuccess(user: User) {
        showMessage("showReadSuccess")
    }

    override fun showError(user: String) {

    }

    override fun showUser(user: User) {
        iv_user_avatar.loadImage(user.headImg)
        tv_user_nick.text = user.nickName
        tv_user_invited_code.text = user.invitationCode
        tv_user_time.text = user.readToday
        tv_user_coin_all.text = user.goldNow
        tv_user_coin_today.text = user.goldToday

    }

    override fun initEvent() {

    }

    override fun lazyFetchData() {

    }

    override fun onStart() {
        super.onStart()
        if (UserController.getInstance().isLogin) {
            mPresenter?.getUser()
        } else {
            tv_user_nick.text = "未登录"
            tv_user_invited_code.text = "--"
            tv_user_time.text = "--"
            tv_user_coin_all.text = "--"
            tv_user_coin_today.text = "--"
        }
    }

    companion object {
        fun newInstance(): UserFragment {
            return UserFragment()
        }
    }

    private val mQMUIInfoDialog: QMUITipDialog by lazy {
        return@lazy QMUITipDialog.Builder(mContext)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_SUCCESS)
                .setTipWord(getString(R.string.dialog_copy_code_success))
                .create()
    }

    override fun setupFragmentComponent(appComponent: AppComponent) {
        DaggerUserComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .userModule(UserModule(this))
                .build()
                .inject(this)
    }

    override fun onResume() {
        super.onResume()
        if (UserController.getInstance().isLogin) {
//            mPresenter?.upDateReadTime(SPUtil.getLong(context, SP_NAME, getTodayStartTime().toString(), 0))

        }
    }


    override fun initView() {

    }

    override fun initView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return inflater.inflate(R.layout.fragment_user, container, false);
    }

    override fun initData(savedInstanceState: Bundle?) {
    }

    override fun setData(data: Any?) {

    }

    override fun showMessage(message: String) {

    }

    @OnClick(R.id.tv_user_invited_code, R.id.btn_user_withdraw, R.id.btn_user_info, R.id.btn_user_record,
            R.id.btn_user_collection, R.id.btn_user_setting, R.id.btn_user_msg, R.id.iv_user_avatar, R.id.btn_user_coin, R.id.btn_user_coin2)
    fun onClick(view: View) {
        if (!UserController.getInstance().isLogin) {
            ArmsUtils.startActivity(LoginActivity::class.java)
        } else {
            when (view.id) {
                R.id.iv_user_avatar -> ArmsUtils.startActivity(UserinfoActivity::class.java)
                R.id.tv_user_invited_code -> {
                    try {
                        RxClipboardTool.copyText(context, UserController.getInstance().loginResultCache.invitationCode)
                        mQMUIInfoDialog.show()
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }

                }
                R.id.btn_user_withdraw -> ArmsUtils.startActivity(WithdrawActivity::class.java)
                R.id.btn_user_info -> ArmsUtils.startActivity(UserinfoActivity::class.java)
                R.id.btn_user_record -> ArmsUtils.startActivity(RecordActivity::class.java)
                R.id.btn_user_collection -> ArmsUtils.startActivity(CollectionActivity::class.java)
                R.id.btn_user_setting -> ArmsUtils.startActivity(SettingActivity::class.java)
                R.id.btn_user_msg -> ArmsUtils.startActivity(MessageActivity::class.java)
                R.id.btn_user_coin -> ArmsUtils.startActivity(WithdrawActivity::class.java)
                R.id.btn_user_coin2 -> ArmsUtils.startActivity(WithdrawActivity::class.java)
            }
        }
    }

    /**
     * 更新金币
     */
    @Subscriber(tag = EventBusTags.UPDATE_GOLD)
    fun updateGold(msg: Message) {
        if (UserController.getInstance().isLogin) {
            mPresenter?.readNews(SPUtil.getLong(context, SP_NAME, getTodayStartTime().toString(), 0), true)
        }
    }

    /**
     * 更新时间
     */
    @Subscriber(tag = EventBusTags.UPDATE_TIME)
    fun updateTime(msg: Message) {
        if (UserController.getInstance().isLogin) {
            mPresenter?.readNews(SPUtil.getLong(context, SP_NAME, getTodayStartTime().toString(), 0), false)
        }
    }

    /**
     * 刷新数据
     */
    @Subscriber(tag = EventBusTags.NEED_UPDATE_DATA)
    fun updateData(msg: Message) {
        if (UserController.getInstance().isLogin) {
            mPresenter?.getUser()
        }
    }


}
